import type { DashboardStats } from '@/types'

// 生成随机数函数
const randomInt = (min: number, max: number) => Math.floor(Math.random() * (max - min + 1)) + min
const randomGrowth = () => (Math.random() * 20 - 10).toFixed(1) // -10% 到 10% 的随机增长

// 生成日期序列
const generateDateRange = (days: number) => {
  const dates = []
  const today = new Date()
  for (let i = days - 1; i >= 0; i--) {
    const date = new Date(today)
    date.setDate(date.getDate() - i)
    dates.push(date.toLocaleDateString('zh-CN', { month: '2-digit', day: '2-digit' }))
  }
  return dates
}

// 生成金额趋势数据
const generateAmountTrend = (days: number) => {
  const dates = generateDateRange(days)
  return dates.map(date => ({
    date,
    amount: randomInt(10000, 50000)
  }))
}

// 业务分布数据
const businessDistribution = [
  { name: '驾校报名', value: randomInt(10000, 50000) },
  { name: '专升本', value: randomInt(8000, 40000) },
  { name: '二手商城', value: randomInt(15000, 60000) },
  { name: '校园外卖', value: randomInt(20000, 80000) },
  { name: '移动业务', value: randomInt(5000, 30000) }
]

// 生成近期交易数据
const generateRecentTransactions = (days: number) => {
  const dates = generateDateRange(days)
  return dates.map(date => ({
    date,
    totalAmount: randomInt(20000, 100000),
    totalCount: randomInt(50, 200),
    drivingAmount: randomInt(5000, 25000),
    educationAmount: randomInt(3000, 15000),
    secondhandAmount: randomInt(8000, 40000),
    takeawayAmount: randomInt(4000, 30000)
  }))
}



// 获取仪表盘统计数据
export const getDashboardStats = (params: { timeRange: string }): Promise<{ data: DashboardStats }> => {
  // 根据时间范围确定天数
  let days = 7
  if (params.timeRange === 'month') days = 30
  if (params.timeRange === 'year') days = 12 // 以月为单位显示
  
  return new Promise(resolve => {
    setTimeout(() => {
      resolve({
        data: {
          userCount: randomInt(5000, 20000),
          userGrowth: parseFloat(randomGrowth()),
          totalAmount: randomInt(500000, 2000000),
          amountGrowth: parseFloat(randomGrowth()),
          vendorCount: randomInt(200, 800),
          vendorGrowth: parseFloat(randomGrowth()),
          schoolCount: randomInt(10, 50),
          schoolGrowth: parseFloat(randomGrowth()),
          todayAmount: randomInt(10000, 50000),
          weekAmount: randomInt(100000, 500000),
          monthAmount: randomInt(500000, 2000000),
          drivingStats: {
            amount: randomInt(100000, 500000),
            count: randomInt(500, 2000)
          },
          educationStats: {
            amount: randomInt(80000, 400000),
            count: randomInt(300, 1500)
          },
          secondhandStats: {
            amount: randomInt(150000, 600000),
            count: randomInt(800, 3000)
          },
          takeawayStats: {
            vendorCount: randomInt(50, 200),
            riderCount: randomInt(100, 500),
            amount: randomInt(200000, 800000),
            count: randomInt(2000, 8000)
          },
          amountTrend: params.timeRange === 'year' 
            ? generateAmountTrend(days).map((item, index) => ({
                date: `${index + 1}月`,
                amount: randomInt(100000, 500000)
              }))
            : generateAmountTrend(days),
          businessDistribution,
          recentTransactions: generateRecentTransactions(7)
        }
      })
    }, 500) // 模拟网络延迟
  })
}